package com.xydtech.ecard.custgroup.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardCustomerGroupOrganize;
import com.xydtech.ecard.custgroup.domain.EcardCustomerOrganizeHandover;
import com.xydtech.ecard.custgroup.domain.EcardCustomerOrganizeTask;
import com.xydtech.ecard.custgroup.mapper.EcardCustomerGroupOrganizeMapper;
import com.xydtech.ecard.custgroup.mapper.EcardCustomerOrganizeHandoverMapper;
import com.xydtech.ecard.custgroup.mapper.EcardCustomerOrganizeTaskMapper;
import com.xydtech.ecard.custgroup.service.IEcardCustomerGroupOrganizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 社团组织信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-11-26 下午3:08
 */
@Service
public class EcardCustomerGroupOrganizeServiceImpl implements IEcardCustomerGroupOrganizeService
{
    @Autowired
    private EcardCustomerGroupOrganizeMapper ecardCustomerGroupOrganizeMapper;
    @Autowired
    private EcardCustomerOrganizeTaskMapper ecardCustomerOrganizeTaskMapper;
    @Autowired
    private EcardCustomerOrganizeHandoverMapper ecardCustomerOrganizeHandoverMapper;

    /**
     * 查询社团组织信息
     *
     * @param id 社团组织信息主键
     * @return 社团组织信息
     */
    @Override
    public EcardCustomerGroupOrganize selectEcardCustomerGroupOrganizeById(Long id, String salt)
    {
        EcardCustomerGroupOrganize ecardCustomerGroupOrganize = ecardCustomerGroupOrganizeMapper.selectEcardCustomerGroupOrganizeById(id);
        try {
            //数据解密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardCustomerGroupOrganize.getOrganizePhone())) {
                ecardCustomerGroupOrganize.setOrganizePhone(AESEncryptionUtils.decrypt(ecardCustomerGroupOrganize.getOrganizePhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardCustomerGroupOrganize;
    }

    /**
     * 查询社团组织信息列表
     *
     * @param ecardCustomerGroupOrganize 社团组织信息
     * @return 社团组织信息
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public List<EcardCustomerGroupOrganize> selectEcardCustomerGroupOrganizeList(EcardCustomerGroupOrganize ecardCustomerGroupOrganize, String salt)
    {
        List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList = ecardCustomerGroupOrganizeMapper.selectEcardCustomerGroupOrganizeList(ecardCustomerGroupOrganize);
        for (EcardCustomerGroupOrganize customerGroupOrganize : ecardCustomerGroupOrganizeList) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(customerGroupOrganize.getOrganizePhone())) {
                    customerGroupOrganize.setOrganizePhone(AESEncryptionUtils.decrypt(customerGroupOrganize.getOrganizePhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ecardCustomerGroupOrganizeList;
    }

    /**
     * 新增社团组织信息
     *
     * @param ecardCustomerGroupOrganize 社团组织信息
     * @return 结果
     */
    @Override
    public int insertEcardCustomerGroupOrganize(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        return ecardCustomerGroupOrganizeMapper.insertEcardCustomerGroupOrganize(ecardCustomerGroupOrganize);
    }

    /**
     * 修改社团组织信息
     *
     * @param ecardCustomerGroupOrganize 社团组织信息
     * @return 结果
     */
    @Override
    public int updateEcardCustomerGroupOrganize(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        return ecardCustomerGroupOrganizeMapper.updateEcardCustomerGroupOrganize(ecardCustomerGroupOrganize);
    }

    /**
     * 批量删除社团组织信息
     *
     * @param ids 需要删除的社团组织信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardCustomerGroupOrganizeByIds(String ids)
    {
        return ecardCustomerGroupOrganizeMapper.deleteEcardCustomerGroupOrganizeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除社团组织信息信息
     *
     * @param id 社团组织信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardCustomerGroupOrganizeById(Long id)
    {
        return ecardCustomerGroupOrganizeMapper.deleteEcardCustomerGroupOrganizeById(id);
    }

    /**
     * 批量移交社团组织信息
     * @param ecardCustomerGroupOrganizeList
     * @param registId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int allocationGroupOrganizeInfo(List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList, Long registId, Long userId) {
        //转交记录
        List<EcardCustomerOrganizeHandover> ecardCustomerOrganizeHandoverList = new ArrayList<>();
        //待办任务
        List<EcardCustomerOrganizeTask> ecardCustomerOrganizeTaskList = new ArrayList<>();

        for(int i = 0; i < ecardCustomerGroupOrganizeList.size(); i++){
            EcardCustomerGroupOrganize ecardCustomerGroupOrganize = ecardCustomerGroupOrganizeList.get(i);
            //查询社团组织信息
            EcardCustomerGroupOrganize oldEcardCustomerGroupOrganize = ecardCustomerGroupOrganizeMapper.selectEcardCustomerGroupOrganizeById(ecardCustomerGroupOrganize.getId());

            EcardCustomerOrganizeHandover ecardCustomerOrganizeHandover = new EcardCustomerOrganizeHandover();
            ecardCustomerOrganizeHandover.setOrganizeId(ecardCustomerGroupOrganize.getId());
            ecardCustomerOrganizeHandover.setTransferId(oldEcardCustomerGroupOrganize.getManagementPersonnelId());
            ecardCustomerOrganizeHandover.setTransferDeptId(oldEcardCustomerGroupOrganize.getDeptId());
            ecardCustomerOrganizeHandover.setRecipientId(ecardCustomerGroupOrganize.getManagementPersonnelId());
            ecardCustomerOrganizeHandover.setRecipientDeptId(ecardCustomerGroupOrganize.getDeptId());
            ecardCustomerOrganizeHandoverList.add(ecardCustomerOrganizeHandover);

            //查询拜访社团组织任务
            EcardCustomerOrganizeTask ecardCustomerOrganizeTask = new EcardCustomerOrganizeTask();
            ecardCustomerOrganizeTask.setOrganizeId(ecardCustomerGroupOrganize.getId());
            ecardCustomerOrganizeTask.setTaskSts("0");
            List<EcardCustomerOrganizeTask> taskList = ecardCustomerOrganizeTaskMapper.selectEcardCustomerOrganizeTaskList(ecardCustomerOrganizeTask);
            //待办任务，最多只有一条
            EcardCustomerOrganizeTask newEcardCustomerOrganizeTask = null;
            if(taskList.size() > 0) {
                EcardCustomerOrganizeTask oldEcardCustomerOrganizeTask = taskList.get(0);
                newEcardCustomerOrganizeTask = new EcardCustomerOrganizeTask();
                newEcardCustomerOrganizeTask.setId(oldEcardCustomerOrganizeTask.getId());
                newEcardCustomerOrganizeTask.setCmRegistId(ecardCustomerGroupOrganize.getManagementPersonnelId());
                newEcardCustomerOrganizeTask.setDeptId(ecardCustomerGroupOrganize.getDeptId());//机构id
                newEcardCustomerOrganizeTask.setCorpId(ecardCustomerGroupOrganize.getCorpId());//法人机构id
                //待办任务list
                ecardCustomerOrganizeTaskList.add(newEcardCustomerOrganizeTask);
            }
        }
        //批量移交社团组织信息
        int a = ecardCustomerGroupOrganizeMapper.batchUpdateEcardCustomerGroupOrganizeInfo(ecardCustomerGroupOrganizeList);
        //批量插入移交记录
        int b = ecardCustomerOrganizeHandoverMapper.batchInsertEcardCustomerOrganizeHandover(ecardCustomerOrganizeHandoverList);

        if(ecardCustomerOrganizeTaskList.size()>0) {
            //批量修改待办任务所属客户经理
            ecardCustomerOrganizeTaskMapper.batchUpdateEcardCustomerOrganizeTask(ecardCustomerOrganizeTaskList);
        }

        return a > b ? b:a;
    }

    /**
     * 批量释放社团组织
     * @param ecardCustomerGroupOrganizeList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchReleaseEcardCustomerGroupOrganize(List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList) {
        //待办任务
        List<EcardCustomerOrganizeTask> ecardCustomerOrganizeTaskList = new ArrayList<>();

        for(int i = 0; i < ecardCustomerGroupOrganizeList.size(); i++){
            EcardCustomerGroupOrganize ecardCustomerGroupOrganize = ecardCustomerGroupOrganizeList.get(i);
            //查询拜访社团组织任务
            EcardCustomerOrganizeTask ecardCustomerOrganizeTask = new EcardCustomerOrganizeTask();
            ecardCustomerOrganizeTask.setOrganizeId(ecardCustomerGroupOrganize.getId());
            ecardCustomerOrganizeTask.setTaskSts("0");
            List<EcardCustomerOrganizeTask> taskList = ecardCustomerOrganizeTaskMapper.selectEcardCustomerOrganizeTaskList(ecardCustomerOrganizeTask);
            //待办任务，最多只有一条
            EcardCustomerOrganizeTask newEcardCustomerOrganizeTask = null;
            if(taskList.size() > 0) {
                EcardCustomerOrganizeTask oldEcardCustomerOrganizeTask = taskList.get(0);
                newEcardCustomerOrganizeTask = new EcardCustomerOrganizeTask();
                newEcardCustomerOrganizeTask.setId(oldEcardCustomerOrganizeTask.getId());
                //释放时任务表客户经理置为null
                newEcardCustomerOrganizeTask.setCmRegistId(null);
                //待办任务list
                ecardCustomerOrganizeTaskList.add(newEcardCustomerOrganizeTask);
            }
        }
        //批量释放社团组织
        int i = ecardCustomerGroupOrganizeMapper.batchReleaseEcardCustomerGroupOrganizeInfo(ecardCustomerGroupOrganizeList);

        if(ecardCustomerOrganizeTaskList.size()>0) {
            //批量释放待办任务
            ecardCustomerOrganizeTaskMapper.batchReleaseEcardCustomerOrganizeTask(ecardCustomerOrganizeTaskList);
        }

        return i;
    }

    /**
     * 批量启用/停用社团组织
     * @param ecardCustomerGroupOrganizeList
     * @return
     */
    @Override
    public int batchSwitchOverEcardCustomerGroupOrganize(List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList) {
        return ecardCustomerGroupOrganizeMapper.batchSwitchOverEcardCustomerGroupOrganize(ecardCustomerGroupOrganizeList);
    }
}
